package com.example.recordingpen_datacollection.Utils.http;

import okhttp3.*;

import java.io.IOException;

import static com.example.recordingpen_datacollection.Utils.http.HttpUrl.*;


public class BDAIokHttpUtil {

    public static String  getBDtoken() throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(BDAIurltoken)
                .build();
        try (Response response = client.newCall(request).execute()) {
           return (response.body().string());
        }
    }

    public static String getBDData(String token,String role,String content) throws IOException {
        String url = BDAIurlData+token;
        OkHttpClient client = new OkHttpClient();
        String jsonBody = "{\"messages\": [{\"role\": \""+role+"\", \"content\": \""+content+"\"}]}";

        RequestBody body = RequestBody.create(jsonBody, MediaType.parse("application/json"));

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                String responseData = response.body().string();
                return responseData;
            } else {
                return response.message();
//                System.out.println("Request failed: " + ;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}